package com.souku.finance.application.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.TextView;

import com.souku.finance.application.R;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ViewUtil;

/**
 * Created by Android on 2016/11/18.
 */

public class CircleViewExp extends View {

    private int cu = 1;
    private double move;
    private int stoke = 6;
    private TextView tv;
    private boolean noPoint;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (move > current) {
                tv.setText(MyTextUtil.setMoneyToElement(current+""));
                if (current == max) {
                    noPoint = true;
                }
                invalidate();
                return;
            }
            cu = cu + 20;
            tv.setText(MyTextUtil.setMoneyToElement(move+""));
            move = current * cu / 1000;
            invalidate();
            handler.sendEmptyMessageDelayed(0, 1);
        }
    };

    public double getCurrent() {
        return current;
    }

    private int width;
    private int height;

    //设定末端颜色
    private int endColor = getResources().getColor(R.color.main);
    //设定背景颜色
    private int backgroundColor = Color.parseColor("#E2E2E2");

    private Paint mPaint1;
    private Paint mPaint2;
    private Paint mPaint3;
    private Paint mPaint4;

    public CircleViewExp(Context context) {
        super(context);
    }

    public CircleViewExp(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint1 = new Paint();
        mPaint1.setColor(backgroundColor);
        mPaint1.setAntiAlias(true);
        mPaint1.setStyle(Paint.Style.STROKE);
        mPaint2 = new Paint();
        mPaint2.setColor(endColor);
        mPaint2.setAntiAlias(true);
        mPaint2.setStyle(Paint.Style.STROKE);
        mPaint2.setStrokeCap(Paint.Cap.ROUND);
        mPaint3 = new Paint();
        mPaint3.setColor(endColor);
        mPaint3.setAntiAlias(true);
        mPaint3.setStyle(Paint.Style.FILL);
        mPaint3.setStrokeCap(Paint.Cap.ROUND);
        mPaint4 = new Paint();
        mPaint4.setColor(Color.parseColor("#FFFFFF"));
        mPaint4.setAntiAlias(true);
        mPaint4.setStyle(Paint.Style.FILL);
        mPaint4.setStrokeCap(Paint.Cap.ROUND);
    }

    public CircleViewExp(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        width = getWidth();
        height = getHeight();
        canvas.rotate(270, width / 2, height / 2);
        int size;
        if (height > width) {
            size = width;
        } else {
            size = height;
        }
        RectF rectF = new RectF(4 * stoke, 4 * stoke, size - 4 * stoke, size - 4 * stoke);
        canvas.drawCircle(size / 2, size / 2, size / 2 - 4, mPaint4);
        canvas.drawArc(rectF, 0, 360, false, mPaint1);
        canvas.drawArc(rectF, stoke / 3, (float) (360 * move / max), false, mPaint2);
        CalcArcEndPointXY(size / 2, size / 2, (size - 8 * stoke) / 2, (float) (360 * move / max));
        if (!noPoint) {
            canvas.drawCircle(posX, posY, 3 * stoke, mPaint3);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private double current;
    private double max = 1;

    public void setStoke(Activity context) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        if (displayMetrics.widthPixels < 800) {
            stoke = 3;
        }
        mPaint2.setStrokeWidth(stoke);
        mPaint1.setStrokeWidth(stoke);
    }

    public void setPrograss(final double current, double max, TextView textView) {
        noPoint=false;
        tv = textView;
        ViewUtil.setTextViewSize(max,tv,true);
        tv.setTextColor(Color.parseColor("#000000"));
        MyTextUtil.setType(getContext(), tv);
        cu = 0;
        move = 0;
        invalidate();
        this.current = current;
        if (current == 0) {
            tv.setText("0");
            return;
        }
        this.max = max;
        handler.sendEmptyMessage(0);
    }

    private float posX;
    private float posY;

    //依圆心坐标，半径，扇形角度，计算出扇形终射线与圆弧交叉点的xy坐标
    public void CalcArcEndPointXY(float cirX, float cirY, float radius, float cirAngle) {

        //将角度转换为弧度
        float arcAngle = (float) (Math.PI * cirAngle / 180.0);
        if (cirAngle < 90) {
            posX = cirX + (float) (Math.cos(arcAngle)) * radius;
            posY = cirY + (float) (Math.sin(arcAngle)) * radius;
        } else if (cirAngle == 90) {
            posX = cirX;
            posY = cirY + radius;
        } else if (cirAngle > 90 && cirAngle < 180) {
            arcAngle = (float) (Math.PI * (180 - cirAngle) / 180.0);
            posX = cirX - (float) (Math.cos(arcAngle)) * radius;
            posY = cirY + (float) (Math.sin(arcAngle)) * radius;
        } else if (cirAngle == 180) {
            posX = cirX - radius;
            posY = cirY;
        } else if (cirAngle > 180 && cirAngle < 270) {
            arcAngle = (float) (Math.PI * (cirAngle - 180) / 180.0);
            posX = cirX - (float) (Math.cos(arcAngle)) * radius;
            posY = cirY - (float) (Math.sin(arcAngle)) * radius;
        } else if (cirAngle == 270) {
            posX = cirX;
            posY = cirY - radius;
        } else {
            arcAngle = (float) (Math.PI * (360 - cirAngle) / 180.0);
            posX = cirX + (float) (Math.cos(arcAngle)) * radius;
            posY = cirY - (float) (Math.sin(arcAngle)) * radius;
        }
    }
}

